/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(function() {

    $('input[type="text"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {

            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    $('input[type="email"]').focus(function() {
       $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {

            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    $("#search_guest").focus(function() {
        $(".slide-page").hide();

    }).blur(function() {
        $(".slide-page").show();
    });
    var user_id = 0;
    if (typeof(Storage) !== "undefined")
    {
        user_id = localStorage.userID;
    }
    //validate phone
    $("#tel_phone").mask("999-999-9999", {placeholder: " "});
    //add guest
    $(".btn-submit-login").click(function() {
        if (checkValidate('error-msg')) {
            $(".btn-submit-login").attr("disabled", "disabled");
            var guest_id = 0;

            if (typeof(Storage) !== "undefined")
            {
                guest_id = sessionStorage.guest_edit_id;
                sessionStorage.guest_edit_id = 0;

            }

            phone= $('input[name="phone"]').val();
            if (phone=="   -   -    "){
                phone='';
            }
            streetAddress = $('input[name="address"]').val();
            if (streetAddress == "Street Address") {
                streetAddress = '';
            }
            city = $('input[name="city"]').val();
            if (city == "City") {
                city = '';
            }
            state = $('input[name="state"]').val();
            if (state == "State") {
                state = '';
            }
            zipcode = $('input[name="zipcode"]').val();
            if (zipcode == "Zipcode") {
                zipcode = '';
            }
            $.post(localStorage.domain_app+"/webservice/guest/updateGuest.php", {
                guest_id: guest_id,
                user_id: user_id,
                firstName: $('input[name="firstname"]').val(),
                lastName: $('input[name="lastname"]').val(),
                email: $('input[name="email"]').val(),
                phone: phone,
                streetAddress: streetAddress,
                city: city,
                state: state,
                zipcode: zipcode

            }, function(res) {

                var num_milliseconds = 3000;
                if (res.error == 1) {
                    $('#error-msg').addClass('active').children().html(res.msg);
                    hideError_2('error-msg', num_milliseconds);

                } else {
                    print_alert(res.msg);
                    wd_redirect("guestList.html");
                }
                $(".btn-submit-login").removeAttr("disabled");
            }, "json");
        }
    });
    //paging
    var killScroll = false;
    $(window).scroll(function() {
        if ($('#end_paging').val() == 0) {
            if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                $('.wd-loading').show();
                if (killScroll == false) {
                    killScroll = true;
                    $.post(localStorage.domain_app+"/webservice/guest/index.php", {
                        page: $('#page').val(),
                        user_id: user_id,
                        keyword: $('#keyword').val(),
                        continue_first_char: $('#continue_first_char').val()

                    }, function(res) {
                        $('.wd-list-guest').append(res.html);
                        $('#page').val(res.page);
                        $('#continue_first_char').val(res.continue_first_char);
                        $('#keyword').val(res.keyword);
                        $('#end_paging').val(res.end_paging);
                        editGuest();
                        deleteGuest();
                        searchGuest();
                        killScroll = false;
                        $('.wd-loading').hide();
                    }, "json");
                } else {
                    return false;
                }
            }
        }
    });


});
function editGuest() {
    $(".wd-edit-guest").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            // Yes! localStorage and sessionStorage support!
            // Some code.....
            sessionStorage.guest_edit_id = $(this).attr('rel');

            return true;

        }
        else
        {
            return false;
        }

    });
}
function actionDeleteGuest(button, guest_id, button_del) {
    if (button == 1) {
        var user_id = 0;
        if (typeof(Storage) !== "undefined")
        {
            user_id = localStorage.userID;
        }
        $.post(localStorage.domain_app+"/webservice/guest/deleteGuest.php", {
            guest_id: guest_id,
            user_id: user_id
        }, function(res) {
            btn_delete = false;
            var num_milliseconds = 3000;
            if (res.error == 1) {
                $('#error-msg').addClass('active').children().html(res.msg);
                hideError_2('error-msg', num_milliseconds);

            } else {
                button_del.closest("li").hide();

            }
        }, "json");
        Ï
    } else {
        btn_delete = false;
        return false;
    }
}
var btn_delete = false;
function deleteGuest() {
    //delete guest

    $(".wd-delete-guest").click(function() {
        var button_del = $(this);
        if (btn_delete == false) {
            btn_delete = true;
            var guest_id = $(this).attr('rel');
            navigator.notification.confirm(
                    "Are you sure you want to delete this guest?",
                    function(buttonIndex) {
                        actionDeleteGuest(buttonIndex, guest_id, button_del);
                    }
            , // callback to invoke with index of button pressed
                    'Wedding Helper', // title
                    'OK,Cancel'          // buttonLabels
                    );

        }


    });
}
function searchGuest() {
    $("#search_guest").keyup(function() {
        var user_id = 0;
        if (typeof(Storage) !== "undefined")
        {
            user_id = localStorage.userID;
        }
        $.post(localStorage.domain_app+"/webservice/guest/index.php", {
            page: 1,
            user_id: user_id,
            keyword: $("#search_guest").val()
        }, function(res) {
            $('.wd-list-guest').html(res.html);
            $('#page').val(res.page);
            $('#continue_first_char').val(res.continue_first_char);
            $('#keyword').val(res.keyword);
            $('#end_paging').val(res.end_paging);
            editGuest();
            deleteGuest();
        }, "json");
    });
}

function checkValidate(id_append) {
    var emailRegex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,
            numericRegex = /^[0-9-]+$/;
    var check_char = /^[a-zA-Z\s]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    errormsg[9] = 'Please type an %s';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="firstname"]').val() == '' || $('input[name="firstname"]').val() == 'First Name') {
        msgEr = errormsg[2].replace('%s', 'first name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="firstname"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="firstname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("First name cannot include special characters");
            hideError(id_append, $('input[name="firstname"]'), num_milliseconds);
            return false;
        } 

    }

    if ($('input[name="lastname"]').val() == '' || $('input[name="lastname"]').val() == 'Last Name') {
        msgEr = errormsg[2].replace('%s', 'last name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="lastname"]'), num_milliseconds);
        return false;
    }
    else {
        valueStr = $('input[name="lastname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Last name cannot include special characters");
            hideError(id_append, $('input[name="lastname"]'), num_milliseconds);
            return false;
        } 

    }


    if ($('input[name="phone"]').val() != '' && $('input[name="phone"]').val() != '   -   -    ' && $('input[name="phone"]').val() != 'Phone Number (###)###-####') {
        if (jQuery.trim($('input[name="phone"]').val()).length < 12) {
        msgEr = errormsg[8].replace('%s', 'cell phone');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="phone"]'), num_milliseconds);
        return false;
        }
    }
    if (($('input[name="email"]').val() == '' || $('input[name="email"]').val() == 'Email Address') && ($('input[name="guest_facebook"]').val()==0)) {
        msgEr = errormsg[9].replace('%s', 'email');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        // hideError(id_append, $('input[name="email"]'), num_milliseconds);
        hideError(id_append, $('input[name="email"]'), num_milliseconds);
        return false;
    } else if (($('input[name="email"]').val() != '') && ($('input[name="email"]').val() != 'Email Address')) {
        valueStr = $('input[name="email"]').val();
        if (!emailRegex.test(valueStr)) {
            msgEr = errormsg[4].replace('%s', 'email');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="email"]'), num_milliseconds);
            return false;
        } 
    }

    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append, num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function isLengthBetween(str, min, max) {
    if (max) {
        return (str.length >= min) && (str.length <= max);
    } else {
        return (str.length >= min);
    }
}